import { useEffect, useRef, useState } from 'react'

function getStorageValue<T>(key: string, value: T) {
  const res = localStorage.getItem(key)
  const save = JSON.parse(res!)
  return save || value
}

// 使用 localStorage
export const useLocalStorage = <T>(key: string, defaultValue?: T): T => {
  useEffect(() => {
    console.log('setItem', defaultValue)
    defaultValue && localStorage.setItem(key, JSON.stringify(defaultValue))
  }, [key, defaultValue])

  return localStorage.getItem(key) ? JSON.parse(localStorage.getItem(key)!) : null
}
